const fs = require('fs');
const axios = require('axios');

const INDENT = '  ',
  LINE_SEPARATOR = '\n',
  INLINE_SEPARATOR = ', ',
  PATH = './src/services',
  TYPES_FILE_NAME = 'api-types.ts',
  BACKEND_SERVER = 'http://localhost:8089',
  FUNCTIONS_FILE_NAME = 'api-functions.ts';
axios.get(`${BACKEND_SERVER}/open/types/list`).then(({ data }) => {
  if (data) {
    const types = data + '\n' + getPageTypeString()
    const path = `${PATH}/${FILE_NAME}`
    if (fs.existsSync(path)) {
      fs.unlinkSync(path)
    }
    if (!fs.existsSync(PATH)) {
      fs.mkdirSync(PATH)
    }
    fs.writeFile(path, types , err => {
      if (err) {
        console.log(err)
      } else {
        console.log(`${FILE_NAME} generated`)
      }
    })
  }
})

function generateTypeScriptTypesStringFromJavaTypes (types) {
  const enums = []
  const interfaces = []
  types.forEach(type => {
    if (type.isEnum) {
      enums.push(type)
    } else {
      interfaces.push(type)
    }
  })
  const sortedEnums = sortEnums(enums)
  return generateEnumStrings(sortedEnums) + '\n' + generateInterfaceString(interfaces)
}

function sortEnums (enums) {
  return [...enums]
}

function generateEnumStrings (sortedEnums) {
  return 'enums: ' + sortedEnums.length
}

function generateInterfaceString (interfaces) {
  return 'interfaces: ' + interfaces.length
}


const getPageTypeString = () => {
  return [
    'export interface Page<T> {',
    '  content: T',
    '  empty: boolean',
    '  first: boolean',
    '  last: boolean',
    '  number: number',
    '  numberOfElements: number',
    '  size: number',
    '  totalElements: number',
    '  totalPages: number',
    '  pageable: {',
    '    offset: number',
    '    pageNumber: number',
    '    pageSize: number',
    '    paged: boolean',
    '    unpaged: boolean',
    '  }',
    '  sort: {',
    '    empty: boolean',
    '    sorted: boolean',
    '    unsorted: boolean',
    '  }',
    '}'
  ].join('\n')
}

const a = '1234'
